package com.cci.kangdao.dao;

import com.cci.kangdao.entity.WorkOrderFlow;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;

/**
 * @author William Du
 * @date 2019/10/18
 */
@MapperScan
public interface WorkOrderFlowTDao {

    //根据工单id获取该工单状态的上一个状态信息   dbu驳回异常关闭申请时回退到异常关闭申请中的上一个状态时用
    //@Select("select top 1 * from WorkOrderFlow_T where OrderId = #{orderId} and CreateTime <(select CreateTime from WorkOrderFlow_T where OrderId = #{orderId} and processStatus = #{orderStatus}) order by CreateTime desc")
    @Select("select top 1 * from WorkOrderFlow_T where OrderId = #{orderId} and CreateTime <\n" +
            "(select top 1 CreateTime from WorkOrderFlow_T where OrderId = #{orderId} and processStatus = #{orderStatus} order by CreateTime desc) order by CreateTime desc")
    Map<String, Object> getAfterWorkOrderFlowByOrderId(@Param("orderId") Long orderId, @Param("orderStatus") int orderStatus);

    @Select("select top 1 * from WorkOrderFlow_T where OrderId = #{orderId} and processStatus = #{orderStatus} and ProcessorId = #{userId}")
    Map<String,Object> getWorkOrderFlowByUserAndStatus(@Param("orderId") Long orderId,@Param("orderStatus") Integer OrderStatus,@Param("userId") Long userId);

    @Select("select top 1 flow.* from WorkOrderFlow_T flow\n" +
            "inner join WorkOrderPrincipal_T principal on principal.Flag = 0 and flow.Flag = 0 and principal.OrderId = flow.OrderId and principal.IsPrincipal = 1 and principal.OrderStatus = flow.processStatus\n" +
            "where flow.OrderId = #{orderId} and flow.processStatus = #{orderStatus}")
    Map<String,Object> getWorkOrderFlowByOrderAndStatus(@Param("orderId") Long orderId,@Param("orderStatus") Integer OrderStatus);

    @Select("select top 1 * from WorkOrderFlow_T where OrderId = #{orderId} and processStatus = #{orderStatus}")
    Map<String,Object> getWorkOrderFlowByOrderIdAndStatus(@Param("orderId") long orderId,@Param("orderStatus") int orderStatus);

    
    List<WorkOrderFlow> getWorkOrderBeforeCloseByOrderId(@Param("orderId") Long orderId);


    //根据工单id和工单状态获取地点
    @Select("select top 1 ISNULL(Location,'') as location  from WorkOrderFlow_T where OrderId = #{orderId} and processStatus = #{orderStatus}")
    String getLocationByOrderIdProcessStatus(@Param("orderId") long orderId, @Param("orderStatus") int orderStatus);

    @Select("select top 1 a.CloseReason,c.Contact from WorkOrder_T a\n" +
            "inner join WorkOrderFlow_T b on a.ID = b.OrderId \n" +
            "inner join User_T c on b.Creator = c.ID \n" +
            "where b.Flag = 0 and c.Flag = 0 and b.processStatus = 31 and a.ID = #{orderId}")
    Map<String, Object> getOrderFlowMsgById(@Param("orderId") long orderId);


    List<WorkOrderFlow> getTechnicianOutAndArrivedTimeByOrderId(@Param("orderId") Long orderId);

    //根据工单获取最新的时间轴数据
    @Select("select top 1 * from WorkOrderFlow_T where OrderId  = #{orderId} order by CreateTime desc")
    Map<String, Object> getTopOneOrderFlowMsgOrderByCreateTimeDesc(@Param("orderId") Long orderId);

    int getCountByOrderId(@Param("orderId") Long orderId);


}
